<?php
class ModelCatalogCategory extends Model {
	public function getCategory($category_id) {
		//$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = '" . (int)$category_id . "' AND cd.language_id = '" . (int)$this->language->getId() . "'");
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c 
				WHERE c.category_id = '" . (int)$category_id . "' AND c.language_id = '" . (int)$this->language->getId() . "'");
		return $query->row;
	}

//---Hoa Tran begin change Oct 16, 2009  ----------------------------------//
	public function getCategories($parent_id = 0,$limit = 1) {
		$SQL  = " SELECT * ";
		$SQL .= "  FROM ".DB_PREFIX . " category c ";
		//$SQL .= "  LEFT JOIN " . DB_PREFIX . " category_description cd ";
		//$SQL .= "    ON (c.category_id = cd.category_id) ";
		$SQL .= "  WHERE c.parent_id = '" . (int)$parent_id . "' ";
		$SQL .= "   AND c.language_id = '".(int)$this->language->getId()."'";
		$SQL .= "  ORDER BY c.date_added ASC ";
		//$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->language->getId() . "' ORDER BY c.sort_order");
		//Le Quoc Nam 11/10/2012 - Get a limit of categories list
		//Modifi By Hai Dang - remove limit for searching by category
		//$SQL .= "LIMIT 0,".$limit;
		$query = $this->db->query($SQL);
		return $query->rows;
	}

	
	public function getCategoriesByShopId($shopID = 0) {
		$SQL  = " SELECT * ";
		$SQL .= "  FROM ".DB_PREFIX . " category c ";
		//$SQL .= "  LEFT JOIN " . DB_PREFIX . " category_description cd ";
		//$SQL .= "    ON (c.category_id = cd.category_id) ";
		$SQL .= "  WHERE c.parent_id = '" . (int)$parent_id . "' ";
		$SQL .= "   AND c.language_id = '".(int)$this->language->getId()."'";
		$SQL .= "  ORDER BY c.date_added ASC ";
		//$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->language->getId() . "' ORDER BY c.sort_order");
		$query = $this->db->query($SQL);
		return $query->rows;
	}

//--------------------------------------Hoa Tran end change Oct 16, 2009--//
	public function getTotalCategoriesByCategoryId($parent_id = 0) {
		/*
		 * Le Quoc Nam 19/10/2012 task improve performance
		 * Add caching
		 */
		$product_data = $this->cache->get('category.totalCategory.' .$parent_id);
		if (!$product_data) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "category WHERE parent_id = '" . (int)$parent_id . "'");
		
		$product_data = $query->row['total'];
		$this->cache->set('category.totalCategory.' . $parent_id,$product_data);
		}
		return $product_data;
	}
}
?>